AssemblerSpriteScale9 = AssemblerSpriteScale9 or Class("AssemblerSpriteScale9", Assembler)

---@diagnostic disable-next-line: duplicate-set-field
function AssemblerSpriteScale9:ctor(sp)
    Assembler.ctor(self, sp)
end

function AssemblerSpriteScale9:getUVs()
    if not self.uvDrity and self._uvs then
        return self._uvs
    end
    local spf = self.renderer.spriteFrame
    local width, height = spf:getNativeSize()
    self._uvs = {}
    local u1 = 0
    local u2 = spf.left / width
    local u3 = (width - spf.right) / width
    local u4 = 1
    local v1 = 0
    local v2 = spf.top / height
    local v3 = (height - spf.bottom) / height
    local v4 = 1
    local uList = {u1, u2, u3, u4}
    local vList = {v1, v2, v3, v4}
    for i = 1, 4 do
        for j = 1, 4 do
            table.insert(self._uvs, uList[i])
            table.insert(self._uvs, vList[j])
        end
    end
    self.uvDirty = false
    return self._uvs
end

function AssemblerSpriteScale9:getVerticePositions()
    if not self._vertexDirty and self._verticePositions then
        return self._verticePositions
    end
    self._vertexDirty = false
    local owner = self.renderer
    local spf = self.renderer.spriteFrame
    local left, top, right, bottom =
        spf.left,
        spf.top,
        spf.right,
        spf.bottom

    local w, h = owner.width, owner.height
    local x1, y1 = owner:localLeft(), owner:localTop()
    -- TODO 处理尺寸小于九宫格的情况
    if w - left - right < 0 then
        local d = left + right - w
        left = left - d / 2
        right = right - d / 2
    end

    if h - top - bottom < 0 then
        local d = top + bottom - h
        top = top - d / 2
        bottom = bottom - d / 2
    end
    local x2 = x1 + left
    local x3 = x1 + w - right
    local x4 = x1 + w

    local y2 = y1 + top
    local y3 = y1 + h - bottom
    local y4 = y1 + h
    
    local xList = {x1, x2, x3, x4}
    local yList = {y1, y2, y3, y4}
    self._verticePositions = {}
    for i = 1, 4 do
        for j = 1, 4 do
            local x, y = xList[i], yList[j]
            x, y = owner:toWorld(x, y)
            table.insert(self._verticePositions, {x, y})
        end
    end
    return self._verticePositions
end

function AssemblerSpriteScale9:getVerticeColors()
    if not self._colorDirty and self._scale9Colors then
        return self._scale9Colors
    end
    self._scale9Colors = {}
    local r, g, b, a = self.renderer:rgba()
    for _ = 1, 16 do
        table.insert(self._scale9Colors, r)
        table.insert(self._scale9Colors, g)
        table.insert(self._scale9Colors, b)
        table.insert(self._scale9Colors, a)
    end
    self._colorDirty = false
    return self._scale9Colors
end

local map = {
    1,2,6,1,6,5,2,3,7,2,7,6,3,4,8,3,8,7,5,6,10,5,10,9,6,7,11,6,11,10,7,8,12,7,12,11,9,10,14,9,14,13,10,11,15,10,15,14,11,12,16,11,16,15
}
function AssemblerSpriteScale9:getVertexMap()
    return map
end

function AssemblerSpriteScale9:setVertexDirty()
    Assembler.setVertexDirty(self)
    self.uvDirty = true
end